package com.android.util;


import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final DateTimeZone ZONE_NY = DateTimeZone.forID("America/New_York");
    public static final DateTimeZone ZONE_BJ = DateTimeZone.forID("Asia/Shanghai");

    private static SimpleDateFormat sf = null;

    public static long getDateTolong(Date date) {
        return date.getTime();
    }

    public static Date getLongToDate(long time) {
        return new Date(time);
    }

    /*获取系统时间 格式为："yyyy-MM-dd HH:mm:ss"*/
    public static String getCurrentDate() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /*获取系统时间 格式为："yyyy-MM-dd"*/
    public static String getCurrentDateYMD() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /*获取系统时间 格式为："yyyy-MM"*/
    public static String getCurrentDateYM() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM");
        return sf.format(d);
    }

    /*获取系统时间 格式为："yyyy"*/
    public static String getCurrentDateY() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy");
        return sf.format(d);
    }

    /*获取系统时间 格式为："MM"*/
    public static String getCurrentDateM() {
        Date d = new Date();
        sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringMD(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("MM-dd");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringMM(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringDD(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("dd");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringYM(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringHm(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("HH:mm");
        return sf.format(d);
    }

    /*时间戳转换成字符窜-时分秒*/
    public static String getDateToStringHms(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("HH:mm:ss");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringYMD(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToMDHm(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("MM-dd HH:mm");
        return sf.format(d);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToStringMDHms(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /*将字符串转为时间戳*/
    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /*将字符串转为时间戳*/
    public static long getStringToDealTime(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr;
        if (time.contains("-0400")) {
            timeStr = time.replace("-0400", "+0800");
        } else {
            timeStr = time.replace("-0500", "+0800");
        }
        Date date = new Date(timeStr);
        return date.getTime();
    }

    /**
     * 将字符串转化为 Date 对象
     *
     * @param dateString 时间字符串 2017-07-27 10:25
     * @return date
     */
    public static Date stringToDate(String dateString) {
        Date dateValue = null;
        try {
            ParsePosition position = new ParsePosition(0);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            dateValue = simpleDateFormat.parse(dateString, position);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateValue;
    }

    /**
     * 将字符串转化为 Date 对象
     *
     * @param dateString 时间字符串 10:25:00
     * @return date
     */
    public static Date stringToDateHMSS(String dateString) {
        Date dateValue = null;
        try {
            ParsePosition position = new ParsePosition(0);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
            dateValue = simpleDateFormat.parse(dateString, position);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateValue;
    }

    //判断是否超过24小时
    public static boolean judgeTime24Hour(long time1) {
        try {
            Date start = new Date(time1);
            Date end = getLongToDate(System.currentTimeMillis());
            long cha = end.getTime() - start.getTime();
            double result = cha * 1.0 / (1000 * 60 * 60);
            if (result <= 24) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param year
     * @param monthOfYear
     * @Description: (根据提供的年月日获取该月份的第一天)
     */
    public static Date getSupportBeginDayofMonth(int year, int monthOfYear) {
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDate = cal.getTime();

        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDate = cal.getTime();
        return firstDate;
    }

    public static boolean isFirstDate(long time) {
        return getLongToDate(time).compareTo(getSupportBeginDayofMonth(Integer.valueOf(getDateToStringMM(time)), Integer.valueOf(getDateToStringDD(time)))) == 0;
    }

    /**
     * @param year
     * @param monthOfYear
     * @Description: (根据提供的年月获取该月份的最后一天)
     */
    public static Date getSupportEndDayofMonth(int year, int monthOfYear) {
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDate = cal.getTime();

        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDate = cal.getTime();
        return lastDate;
    }

    /**
     * 判断给定字符串时间是否为今日
     *
     * @param date
     * @return boolean
     */
    public static boolean isToday(String date) {
        boolean b = false;
        Date time = toDate(date);
        Date today = new Date();
        if (time != null) {
            String nowDate = dateFormater2.get().format(today);
            String timeDate = dateFormater2.get().format(time);
            if (nowDate.equals(timeDate)) {
                b = true;
            }
        }
        return b;
    }

    /**
     * 将字符串转为日期类型
     *
     * @param date
     * @return
     */
    public static Date toDate(String date) {
        try {
            return dateFormater.get().parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    /**
     * @return 当前时间加上一分钟的时间戳
     */
    public static long getCurrentTimeAddAMinute() {
        return setCurrentTimeAddMinute(1);
    }

    /**
     * 设置当前时间加上分钟数
     *
     * @return 最终时间戳
     */
    private static long setCurrentTimeAddMinute(int minute) {
        DateTime dateTime = DateTime.now().plusMinutes(minute);
        return dateTime.toDate().getTime();
    }

    public static String getDateToYMD(long time) {
        /*时间戳转换成字符窜*/
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy/MM/dd");
        return sf.format(d);
    }

}